package com.jstyle.jclifexd.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.android.flexbox.FlexboxLayoutManager;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.utils.ResolveData;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/6/13.
 */

public class HomePageAdapter extends RecyclerViewBaseAdapter<String> {

    String[]datas=new String[]{"0","0","0","00:00","0"};
    public HomePageAdapter(List<String> mDatas) {
        super(mDatas);
    }

    @Override
    protected void bindData(BaseViewHolder holder, int position) {
        HomePageViewHolder homePageViewHolder=(HomePageViewHolder)holder;
        if(position==1){
            homePageViewHolder.btHomepage.setText(datas[position]+" \n" + ResolveData.getHomeDistanceLabel());
        }else{
            homePageViewHolder.btHomepage.setText(datas[position]+" \n" + mDataList.get(position));

        }
        setDrawableLeft(homePageViewHolder, position);
        homePageViewHolder.viewHorizontal.setVisibility(position==4||position==2?View.INVISIBLE:View.VISIBLE);
        homePageViewHolder.viewVertical.setVisibility(position>2?View.INVISIBLE:View.VISIBLE);
        ViewGroup.LayoutParams lp = ((RelativeLayout) (  homePageViewHolder.btHomepage.getParent())).getLayoutParams();
        if (lp instanceof FlexboxLayoutManager.LayoutParams) {
            FlexboxLayoutManager.LayoutParams flexboxLp = (FlexboxLayoutManager.LayoutParams) lp;
            flexboxLp.setFlexBasisPercent(position < 3 ? 0.333f : 0.5f);//写1/3部分机型平分不了
        }


    }

    public void setDatas(String[]datas){
        this.datas=datas;
        notifyDataSetChanged();
    }
    private void setDrawableLeft(HomePageViewHolder viewHolder, int position) {
       Context context=viewHolder.itemView.getContext();
        int id = 0;
        switch (position) {
            case 0:
                id = R.drawable.home_cal;
                break;
            case 1:
                id = R.drawable.home_distance;
                break;
            case 2:
                id = R.drawable.home_heart;
                break;
            case 3:
                id = R.drawable.home_time;
                break;
            case 4:
                id = R.drawable.home_exercise_time;
                break;
        }
        Drawable drawable = context.getResources().getDrawable(id);
        if (drawable != null) {
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            viewHolder.btHomepage.setCompoundDrawables(drawable, null, null, null);
            if(position==3){
                viewHolder.itemView.setBackground(context.getResources().getDrawable(R.drawable.ripple_left));
            }else if(position==4){
                viewHolder.itemView.setBackground(context.getResources().getDrawable(R.drawable.ripple_right));
            }else{
                viewHolder.itemView.setBackground(context.getResources().getDrawable(R.drawable.ripple_certen));
            }
        }
    }

    @Override
    protected int getLayoutId(int viewType) {
        return R.layout.item_homepage;
    }

    public class HomePageViewHolder extends BaseViewHolder {
        @BindView(R.id.bt_homepage)
        TextView btHomepage;

        @BindView(R.id.view_Horizontal)
        View viewHorizontal;
        @BindView(R.id.view_Vertical)
        View viewVertical;

        public HomePageViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new HomePageViewHolder(LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType),parent,false));
    }

    public String[]getDatas(){
        return this.datas;
    }
}
